package ru.alexo.whiskey.events;

import java.io.IOException;
import java.io.OutputStream;

/**
 * A base music event.
 * 
 * @author Exaybachay
 */
public abstract class MusicEvent implements TrackEvent {

    /**
     * Support only 1 channel for now.
     */
    public final byte NOTE_ON = (byte) 0x90;
    public final byte NOTE_OFF = (byte) 0x80;
    public final byte DEFAULT_VOLUME = (byte) 100;

    protected byte velocity;
    protected byte duration;

    public MusicEvent(byte velocity, byte duration) {
	this.velocity = velocity;
	this.duration = duration;
    }

    public byte getVelocity() {
	return velocity;
    }

    public void setVelocity(byte velocity) {
	this.velocity = velocity;
    }

    public byte getDuration() {
	return duration;
    }

    public void setDuration(byte duration) {
	this.duration = duration;
    }

    protected void sendLength(int x, OutputStream outputSream) throws IOException {
	if (x >= 2097152) {
	    outputSream.write((byte) (128 + x / 2097152));
	    x %= 2097152;
	}
	if (x >= 16384) {
	    outputSream.write((byte) (128 + x / 16384));
	    x %= 16384;
	}
	if (x >= 128) {
	    outputSream.write((byte) (128 + x / 128));
	    x %= 128;
	}
	outputSream.write((byte) x);
    }

}